<?php
declare(strict_types=1);

namespace app\admin\service;

use app\admin\model\Demand as DemandModel;
use app\admin\model\ResourceSaleClickShare;

class DemandService
{
    /**
     * 今日新增
     * @return int
     */
    public static function todayDemand()
    {
        return DemandModel::todayDemand();
    }

    /**
     * 本周新增需求
     * @return int
     */
    public static function countDemandWeek()
    {
        return DemandModel::countDemandWeek();
    }

    /**
     * 本月新增需求
     * @return int
     */
    public static function countDemandMonth()
    {
        return DemandModel::countDemandMonth();
    }

    /**
     * 获取本月需求（折线图）
     * @return mixed
     */
    public static function getDemandMonthLine()
    {
        $startEndTime = getStartEndTime();
        $usersMonth = DemandModel::getDemandMonth($startEndTime['startTime'], $startEndTime['endTime']);
        return inArr($usersMonth, $startEndTime['dateArr']);
    }

    /**
     * 需求统计
     * @param array $where
     * @return int
     */
    public static function countDemand($where = [])
    {
        if (!$where) {
            $where[] = ['status', '<>', -1];
        }
        return DemandModel::countDemand($where);
    }

    /**
     * 需求列表
     * @param $where
     * @param $page
     * @param $pagesize
     * @param $type
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getDemandList($where, $page, $pagesize, $type)
    {
        $list = DemandModel::getDemandList($where, $page, $pagesize);

        $list = self::assembleList($list, $type);

        return $list;
    }

    /**
     * 统计需求列表
     * @param $where
     * @return mixed
     */
    public static function countDemandList($where)
    {
        return DemandModel::countDemandList($where);
    }

    /**
     * 组装列表数据
     * @param $list
     * @param $type 1=>交易中  2=>已售出  3=>待完善
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected static function assembleList($list, $type)
    {
        foreach ($list as $k => $v) {
            if ($type == 3) {
                if (strrpos($v['city_name'], '市')) {
                    $list[$k]['city_name'] = substr_replace($v['city_name'], '', strrpos($v['city_name'], '市'));
                }
            } else {

                $list[$k]['ids'] = $v['id'] . '<br><span class="color"> ' . $v['s_number'] . '</span>';
                // 已售出，查询订单编号
                $orders = [];
                if ($v['status'] == 2) {
                    $orders = OrderService::findOrder(['mid' => $v['id'], 'type_id' => 16]);
                }
                $pay_time = 0;
                if ($orders) {
                    $list[$k]['ids'] .= '<br>' . $orders['order_no'];
                    $pay_time = date('Y-m-d H:i', $orders['pay_time']);
                }
                // LOGO
                $logo = '<div class="mar-lefts"><img class="otherimages" title="点我查看原图" onclick="lookimg(this.src)" src="' . config('url.url') . $v['logo'] . '" \' + \'alt=""></div>';
                // 名称数据
                $list[$k]['name_data'] = '<div class="d-fix1">' . $logo . '<p class="datas">' . $v['name'] . '/<span class="color3">' . $v['label'] . '</span></p></div>';

                // 二维码
                $qrcode = '<div><img class="otherimages" title="点我查看原图" onclick="lookimg(this.src)" src="' . config('url.url') . $v['qrcode'] . '" \' + \'alt=""></div>';
                if (strrpos($v['city_name'], '市')) {
                    $v['city_name'] = substr_replace($v['city_name'], '', strrpos($v['city_name'], '市'));
                }
                // 企业数据
                $list[$k]['company_data'] = '<p>' . $v['group_number'] . '/<span class="color3">' . $v['city_name'] . '</span></p>';


                // 用户数据
//                $users = UsersService::findUsers(['id' => $v['uid']]);
//                $headimg = '<div><img class="headimgs" title="点我查看原图" onclick="lookimg(this.src)" src="' . $users['headimg'] . '" \' + \'alt=""></div>';
                $users = $v['uid'] ? UsersService::findUsers(['id' => $v['uid']]) : '官方联系人员';
                $list[$k]['users_data'] = '<p style="text-align: center" class="datas spaces">' . (isset($users['nickname']) ? emojiDecode($users['nickname']) : $users) . '</p>';

                // 创建数据
                $accessCount = ResourceSaleClickShare::countResourceSaleClickShare(['sid' => $v['id'], 'type' => 1]);

                $list[$k]['create_time'] = date('Y-m-d H:i', (is_string($v['create_time']) ? strtotime($v['create_time']) : $v['create_time']));

                $create_time = $list[$k]['create_time'] . '<br>';
                if ($pay_time) {
                    $create_time .= '<span class="color">' . $pay_time . '</span><br>';
                }
                $create_time .= '<span class="color3">' . $accessCount . '</span>';
                $list[$k]['create_data'] = $create_time;

                $feedback = ResourceSaleFeedbackService::findResourceSaleFeedback([
                    'f.sid' => $v['id'],
                    's.type' => 1
                ]);
                $list[$k]['isFeedback'] = 0;
                $list[$k]['feedback'] = '';
                if ($feedback) {
                    $list[$k]['isFeedback'] = 1;
                    $feedback['images'] = $feedback['images'] ? config('url.url') . $feedback['images'] : '';
                    $feedback['reply_time'] = $feedback['reply_time'] ? date('Y-m-d H:i:s', $feedback['reply_time']) : '';
                    $list[$k]['feedback'] = $feedback;
                }
            }
        }

        return $list;
    }

    /**
     * 获取最后一个自增id
     * @return mixed
     */
    public static function findDemandLastId()
    {
        return DemandModel::findDemandLastId();
    }

    /**
     * 单需求数据
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function findDemand($where)
    {
        return DemandModel::findDemand($where);
    }

    /**
     * 创建需求数据
     * @param $data
     * @return DemandModel
     */
    public static function createDemand($data)
    {
        return DemandModel::createDemand($data);
    }

    /**
     * 更新需求数据
     * @param $data
     * @param $where
     * @return DemandModel
     */
    public static function updateDemand($data, $where)
    {
        return DemandModel::updateDemand($data, $where);
    }

    /**
     * 需求总金额
     * @param $where
     * @return float
     */
    public static function sumDemandPrice($where)
    {
        return DemandModel::sumDemandPrice($where);
    }
}